package com.springbootmybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.springbootmybatis.dto.LoginDto;
import com.springbootmybatis.entity.User;
import com.springbootmybatis.service.UserService;
import com.springbootmybatis.utils.JSONResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userServiceImpl;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginPostDto(LoginDto loginDto){
        return loginDto.toString();
    }

    /**
     * 通过userId查询用户
     * @param userId
     * @return
     */
    @RequestMapping("/query-by-userId")
    public String queryByUserId(String userId){
        logger.info("/query,userId={}", userId);
        JSONObject jsonObject = new JSONObject();
        try {
            User user = userServiceImpl.selectByUserId(userId);
            JSONResultHandler.handlerObject(jsonObject, "0", "查询成功", user);
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 通过userId删除用户
     * @param userId
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String userId){
        logger.info("/delete,userId={}", userId);
        JSONObject jsonObject = new JSONObject();
        try {
            userServiceImpl.deleteByUserId(userId);
            JSONResultHandler.handlerObject(jsonObject, "0", "删除成功");
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 普通用户注册，返回生成的userId
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestBody User user){
        logger.info("/add, user={}", user);
        JSONObject jsonObject = new JSONObject();
        try {
            if (user == null){
                return JSONResultHandler.handler(jsonObject, "-1001", "参数不能为空");
            }
            user.setUserLevel(2);
            String userId = userServiceImpl.insert(user);
            JSONObject data = new JSONObject();
            data.put("userId", userId);
            JSONResultHandler.handlerObject(jsonObject, "0", "新增成功", data);
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody User user){
        logger.info("/update,user={}", user);
        JSONObject jsonObject = new JSONObject();
        try {
            if (user == null){
                return JSONResultHandler.handler(jsonObject, "-1001", "参数不能为空");
            }
            userServiceImpl.update(user);
            JSONResultHandler.handlerObject(jsonObject, "0", "更新成功", user);
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

}
